package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/checkJoinCompanyStatus")
public class CheckJoinCompanyStatusServlet extends HttpServlet {
    private final CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前用户
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");
            
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户未登录");
                out.write(gson.toJson(result));
                return;
            }
            
            // 获取用户最新的申请
            CompanyEmployee application = companyEmployeeService.getUserLatestApplication(user.getId());
            
            // 检查用户是否已在公司
            boolean isInCompany = companyEmployeeService.canUseFeatures(user.getId());
            
            result.put("success", true);
            result.put("inCompany", isInCompany);
            
            // 不管用户是否在公司，都返回申请信息
            result.put("application", application);
            
            // 如果用户已在公司，需要更新会话中的用户对象以包含公司ID和员工编号
            if (isInCompany) {
                if (user.getCompanyId() == null || user.getEmployeeNumber() == null) {
                    // 更新user对象（这里简化处理，实际可能需要从数据库重新查询完整的用户信息）
                    if (application != null && application.getStatus() == 1) {
                        user.setCompanyId(application.getCompanyId());
                        user.setEmployeeNumber(application.getEmployeeNumber());
                        session.setAttribute("user", user);
                    }
                }
            } else {
                // 如果用户不在公司，清除其会话中的公司相关信息
                user.setCompanyId(null);
                user.setEmployeeNumber(null);
                session.setAttribute("user", user);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "获取申请状态失败：" + e.getMessage());
        }
        
        out.write(gson.toJson(result));
        out.flush();
    }
} 